package com.example.progresshint;

import com.warkiz.widget.IndicatorStayLayout;

import io.techery.progresshint.ProgressHintDelegate;
import io.techery.progresshint.addition.widget.SeekBar;
import io.techery.progresshint.addition.widget.VerticalSeekBar;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import ohos.multimodalinput.event.TouchEvent;

import java.util.Arrays;
import java.util.List;

import static io.techery.progresshint.ProgressHintDelegate.POPUP_FIXED;
import static io.techery.progresshint.ProgressHintDelegate.POPUP_FOLLOW;

public class MainAbility extends Ability implements Component.ClickedListener {
    /**
     * 菜单动画时间
     */
    private static final int ANIMATOR_DURATION = 200;
    /**
     * 菜单显示TAG
     */
    private static final int SHOW_MENU = 100;
    /**
     * 菜单隐藏TAG
     */
    private static final int HIDE_MENU = 200;

    private List<SeekBar> seekBars;
    private SeekBar mHorSeekbar1;
    private IndicatorStayLayout mHorStayLayout1;
    private SeekBar mHorSeekbar2;
    private IndicatorStayLayout mHorStayLayout2;

    private VerticalSeekBar mVerSeekbar1;
    private VerticalSeekBar mVerSeekbar2;

    private DependentLayout mIcMenuLayout;
    private DirectionalLayout mMenuLayout;
    private Component mBgCloseMenu;
    private AnimatorProperty mMenuLayoutHideAnimator;
    private AnimatorProperty mMenuLayoutShowAnimator;

    private MyEventHandler mHandler = new MyEventHandler(EventRunner.getMainEventRunner());

    /**
     * MyEventHandler
     */
    private class MyEventHandler extends EventHandler {
        private MyEventHandler(EventRunner runner) {
            super(runner);
        }

        @Override
        public void processEvent(InnerEvent event) {
            super.processEvent(event);
            switch (event.eventId) {
                case SHOW_MENU:
                    if (mMenuLayout != null && mBgCloseMenu != null) {
                        mMenuLayout.setVisibility(Component.VISIBLE);
                        mBgCloseMenu.setVisibility(Component.VISIBLE);
                    }
                    break;
                case HIDE_MENU:
                    if (mMenuLayout != null && mBgCloseMenu != null) {
                        mMenuLayout.setVisibility(Component.HIDE);
                        mBgCloseMenu.setVisibility(Component.HIDE);
                    }
                    break;
            }
        }
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(Color.BLACK.getValue());
        super.setUIContent(ResourceTable.Layout_ability_main);

        mHorSeekbar1 = (SeekBar) findComponentById(ResourceTable.Id_seekbar_horizontal1);
        mHorStayLayout1 = (IndicatorStayLayout) findComponentById(ResourceTable.Id_staylayout_horizontal1);
        mHorStayLayout1.layoutIndicator(mHorSeekbar1, 0);
        mHorStayLayout1.setMarginsLeftAndRight(
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar1).getMarginLeftByPercent(),
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar1).getMarginRightByPercent());
        mHorStayLayout1.setMarginsTopAndBottom(
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar1).getMarginTopByPercent(),
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar1).getMarginBottomByPercent());

        mHorSeekbar2 = (SeekBar) findComponentById(ResourceTable.Id_seekbar_horizontal2);
        mHorStayLayout2 = (IndicatorStayLayout) findComponentById(ResourceTable.Id_staylayout_horizontal2);
        mHorStayLayout2.layoutIndicator(mHorSeekbar2, 0);
        mHorStayLayout2.setMarginsLeftAndRight(
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar2).getMarginLeftByPercent(),
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar2).getMarginRightByPercent());
        mHorStayLayout2.setMarginsTopAndBottom(
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar2).getMarginTopByPercent(),
                ((ProgressHintDelegate.SeekBarHintDelegateHolder)mHorSeekbar2).getMarginBottomByPercent());

        seekBars = Arrays.asList(mHorSeekbar1, mHorSeekbar2);

        mVerSeekbar1 = (VerticalSeekBar) findComponentById(ResourceTable.Id_verSeekbar1);
        mVerSeekbar2 = (VerticalSeekBar) findComponentById(ResourceTable.Id_verSeekbar2);

        initMenu();
    }

    private void initMenu() {
        mIcMenuLayout = (DependentLayout) findComponentById(ResourceTable.Id_ic_menu_layout);
        mMenuLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_menu_layout);
        mMenuLayout.setTouchEventListener((component, touchEvent) -> true);
        mBgCloseMenu = findComponentById(ResourceTable.Id_bg_closemenu);

        mIcMenuLayout.setClickedListener(component -> {
            setMenuVisible(true);
        });
        mBgCloseMenu.setTouchEventListener((component, touchEvent) -> {
            if (mBgCloseMenu.getVisibility() != Component.HIDE) {
                if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_UP) {
                    setMenuVisible(false);
                }
                return true;
            }
            return false;
        });

        initMenuAnimator();
        findComponentById(ResourceTable.Id_bt_fixed).setClickedListener(this::onClick);
        findComponentById(ResourceTable.Id_bt_follow).setClickedListener(this::onClick);
        findComponentById(ResourceTable.Id_bt_open_scroll).setClickedListener(this::onClick);
    }

    /**
     * 设置菜单栏显示/隐藏
     *
     * @param visible 控件是否可见
     */
    private void setMenuVisible(boolean visible) {
        if (visible) {
            mHandler.sendEvent(SHOW_MENU);
            mMenuLayoutShowAnimator.start();
        } else {
            mMenuLayoutHideAnimator.start();
            mHandler.sendEvent(HIDE_MENU, ANIMATOR_DURATION);
        }
    }

    /**
     * 设置menu消失的动画
     */
    private void initMenuAnimator() {
        mMenuLayoutHideAnimator = mMenuLayout.createAnimatorProperty();
        mMenuLayoutHideAnimator.alpha(0).setDuration(ANIMATOR_DURATION);
        mMenuLayoutShowAnimator = mMenuLayout.createAnimatorProperty();
        mMenuLayoutShowAnimator.alphaFrom(0).alpha(1).setDuration(ANIMATOR_DURATION);
    }

    @Override
    public void onClick(Component component) {
        int style = POPUP_FIXED;
        setMenuVisible(false);
        switch (component.getId()) {
            case ResourceTable.Id_bt_fixed:
                style = POPUP_FIXED;
                mVerSeekbar1.setFllow(false);
                mVerSeekbar2.setFllow(false);
                break;
            case ResourceTable.Id_bt_follow:
                style = POPUP_FOLLOW;
                mVerSeekbar1.setFllow(true);
                mVerSeekbar2.setFllow(true);
                break;
            case ResourceTable.Id_bt_open_scroll:
                jumpAbility();
                return;
            default:
                break;
        }
        for (SeekBar seekBar : seekBars) {
            seekBar.setPopupStyle(((ProgressHintDelegate.SeekBarHintDelegateHolder)seekBar).getMarginLeftByPercent(),
                    ((ProgressHintDelegate.SeekBarHintDelegateHolder)seekBar).getMarginRightByPercent(),
                    style);
        }
    }

    private void jumpAbility() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.example.progresshint")
                .withAbilityName("com.example.progresshint.ScrollAbility")
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    @Override
    protected void onBackPressed() {
        if (mMenuLayout.getVisibility() == Component.VISIBLE) {
            setMenuVisible(false);
            return;
        }
        super.onBackPressed();
    }
}
